package com.njlime.core.helper.dic;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 数据字典工具类<br>
 * Created by Dean on 2017-9-5.
 */
public class DictionaryHelper {


    private static DictionaryHelper dh;
    private Map<String, Map<Object, String>> groups = new HashMap<String, Map<Object, String>>();

    public static DictionaryHelper getInstance() {
        if (dh != null) {
            return dh;
        }
        dh = new DictionaryHelper();
        return dh;
    }

    /**
     * 添加字典
     * @param groupName 组名称
     * @param display 显示名称
     * @param value 值
     */
    public void putDic(String groupName, String display, Object value) {
        if (groupName == null || display == null) {
            return;
        }
        Map<Object, String> group = groups.get(groupName);
        if (group == null) {
            group = new LinkedHashMap<Object,String>();
            groups.put(groupName, group);
        }
        if (group.containsKey(value)) {
            return;
        }
        group.put(value, display);
    }

    /**
     * 获取字典组
     * @param groupName
     * @return
     */
    public Map<Object, String> getGroup(String groupName) {
        return groups.get(groupName);
    }

    /**
     * 移除字典
     * @param groupName
     */
    public void removeGroup(String groupName) {
        groups.remove(groupName);
    }

    /**
     * 获取字典值
     * @param groupName
     * @param display
     * @return
     */
    public Object getDicValue(String groupName, String display) {
        Map<Object, String> group = groups.get(groupName);
        if (group == null) {
            return null;
        }
        for (Entry<Object, String> option : group.entrySet()) {
            if (option.getValue().equals(display)) {
                return option.getKey();
            }
        }
        return null;
    }

    /**
     * 获取字典名称
     * @param groupName
     * @param value
     * @return
     */
    public String getDicDisplay(String groupName, Object value) {
        Map<Object, String> group = groups.get(groupName);
        if (group == null) {
            return null;
        }
        return group.get(value);
    }

}
